package com.ruoyi.cms.service.impl;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.cms.domain.CmsLink;
import com.ruoyi.cms.domain.bo.CmsLinkBo;
import com.ruoyi.cms.domain.vo.CmsLinkVo;
import com.ruoyi.cms.mapper.CmsLinkMapper;
import com.ruoyi.cms.mapper.CmsWebsiteMapper;
import com.ruoyi.cms.service.ICmsLinkService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import lombok.RequiredArgsConstructor;

/**
 * 友情链接Service业务层处理
 *
 * @author guorui
 * @date 2023-11-08
 */
@RequiredArgsConstructor
@Service
public class CmsLinkServiceImpl implements ICmsLinkService {

    private final CmsLinkMapper baseMapper;
    
    private final CmsWebsiteMapper websiteMapper;

    /**
     * 查询友情链接
     */
    @Override
    public CmsLinkVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询友情链接列表
     */
    @Override
    public TableDataInfo<CmsLinkVo> queryPageList(CmsLinkBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<CmsLink> lqw = buildQueryWrapper(bo);
        Page<CmsLinkVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        // 关联查询
//        if(CollUtil.isNotEmpty(result.getRecords())) {
//        	result.getRecords().stream().map(new Function<CmsLinkVo, CmsLinkVo>() {
//        		
//        		public CmsLinkVo apply(CmsLinkVo item) {
//        			item.setWebsite(websiteMapper.selectById(item.getSiteId()).getName());
//                    return item;
//                }
//        		
//        	}).collect(Collectors.toList());
//        }
        return TableDataInfo.build(result);
    }

    /**
     * 查询友情链接列表
     */
    @Override
    public List<CmsLinkVo> queryList(CmsLinkBo bo) {
        LambdaQueryWrapper<CmsLink> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<CmsLink> buildQueryWrapper(CmsLinkBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<CmsLink> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getSiteId() != null, CmsLink::getSiteId, bo.getSiteId());
        lqw.eq(bo.getModId() != null, CmsLink::getModId, bo.getModId());
        lqw.like(StringUtils.isNotBlank(bo.getName()), CmsLink::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getSrc()), CmsLink::getSrc, bo.getSrc());
        lqw.eq(bo.getTarget() != null, CmsLink::getTarget, bo.getTarget());
        lqw.eq(bo.getStatus() != null, CmsLink::getStatus, bo.getStatus());
        lqw.eq(bo.getSort() != null, CmsLink::getSort, bo.getSort());
        return lqw;
    }

    /**
     * 新增友情链接
     */
    @Override
    public Boolean insertByBo(CmsLinkBo bo) {
        CmsLink add = BeanUtil.toBean(bo, CmsLink.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改友情链接
     */
    @Override
    public Boolean updateByBo(CmsLinkBo bo) {
        CmsLink update = BeanUtil.toBean(bo, CmsLink.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(CmsLink entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除友情链接
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
